package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.irozon.library.HideKey;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerHouseRentingShowMainComponent;
import com.kmproject.haifu.mvp.contract.HouseRentingShowMainContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseRentListParmarBean;
import com.kmproject.haifu.mvp.presenter.HouseRentingShowMainPresenter;
import com.kmproject.haifu.mvp.ui.adapter.HouseRentingShowListAdapter;
import com.kmproject.haifu.mvp.ui.adapter.HouseRentingShowMainItemNewAdapter;
import com.library.flowlayout.SpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.xw.repo.XEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/03/2019 11:31
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HouseRentingShowMainActivity extends BaseActivity<HouseRentingShowMainPresenter> implements HouseRentingShowMainContract.View {

    @BindView(R.id.include_title_search_back)
    ImageView includeTitleSearchBack;
    @BindView(R.id.include_title_search_xedit)
    XEditText includeTitleSearchXedit;
    @BindView(R.id.include_title_search_msg)
    ImageView includeTitleSearchMsg;
    @BindView(R.id.include_title_search_lay)
    LinearLayout includeTitleSearchLay;
    @BindView(R.id.HouseRentingShowMain_RecyclerView)
    RecyclerView HouseRentingShowMainRecyclerView;
    @BindView(R.id.HouseRentingShowMain_refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.popuwindow_selectsearchaddress_searchRootlay)
    LinearLayout popuwindowSelectsearchaddressSearchRootlay;
    private HouseRentingShowListAdapter contentAdapter;

    RecyclerView headRecyclerView;
    HouseRentingShowMainItemNewAdapter menuAdapter;
    HouseRentListParmarBean houseRentListParmarBean = new HouseRentListParmarBean();

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHouseRentingShowMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_house_renting_show_main; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        HideKey.initialize(this);
        includeTitleSearchXedit.setFocusable(false);
        ArmsUtils.configRecyclerView(HouseRentingShowMainRecyclerView, new LinearLayoutManager(this));
        contentAdapter = new HouseRentingShowListAdapter(new ArrayList<>());
        contentAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        HouseRentingShowMainRecyclerView.setAdapter(contentAdapter);
        //刷新
        mRefreshLayout.setEnableRefresh(false);
        //加载更多
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //    houseRentListParmarBean.
                mPresenter.getHouseRentList(houseRentListParmarBean);
            }
        });


        View headView = LayoutInflater.from(this).inflate(R.layout.houserenting_main_headview, null);
        headRecyclerView = headView.findViewById(R.id.houserenting_main_headview_RecyclerView);
        headRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        headRecyclerView.addItemDecoration(new SpaceItemDecoration(AppUtil.dp2px(10)));
        menuAdapter = new HouseRentingShowMainItemNewAdapter(new ArrayList<>());
        headRecyclerView.setAdapter(menuAdapter);
        contentAdapter.addHeaderView(headView);
        mPresenter.dictList();
        houseRentListParmarBean.setIsRecommend("1");
        houseRentListParmarBean.setLimit("5");//显示20条推荐
        mPresenter.getHouseRentList(houseRentListParmarBean);
    }

    @Override
    protected void onResume() {
        super.onResume();
        includeTitleSearchXedit.setEnabled(true);
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DefaultAdapter.releaseAllHolder(HouseRentingShowMainRecyclerView);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.popuwindow_selectsearchaddress_searchRootlay,R.id.include_title_search_xedit, R.id.include_title_search_lay, R.id.include_title_search_back, R.id.include_title_search_msg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_title_search_back:

                killMyself();
                break;
            case R.id.include_title_search_xedit:
                includeTitleSearchXedit.setEnabled(false);
                mPresenter.showPopuWindowToSearch(this, includeTitleSearchLay, menuAdapter.getData());
                break;
            case R.id.include_title_search_msg:
                //消息
              AppUtil.gotoMessage(this);
                break;
            case R.id.popuwindow_selectsearchaddress_searchRootlay:
                mPresenter.showPopuWindowToSearch(this, includeTitleSearchLay, menuAdapter.getData());
                break;
        }
    }

    @Override
    public void dictListSuccess(List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            for (HousePayWayBean housePayWayBean : housePayWayBeans) {
                for (HousePayWayBean child : housePayWayBean.getChildren()) {
                    child.setParentCategory(housePayWayBean.getCategory());
                    menuAdapter.addData(child);
                }

            }
        }
    }

    @Override
    public void getHouseRentListSuccess(ResultBean<List<HouseRentListShowBean>> houseRentListShowBeans) {
                loadingDialog.dismiss();
        mRefreshLayout.finishLoadMore();
        if (houseRentListShowBeans == null || houseRentListShowBeans.getData() == null || houseRentListShowBeans.getData().isEmpty()) {
            if (contentAdapter.getData() == null || contentAdapter.getData().isEmpty()) {
                //第一次就没有数据
                contentAdapter.setEmptyView(AppUtil.setEmptyView(HouseRentingShowMainActivity.this, "没有推荐信息！", "", null, true));
            }
            mRefreshLayout.setEnableLoadMore(false);//不能刷新了
            return;
        }

        if (houseRentListShowBeans.getData().size() < AppConstant.pageSize || Integer.parseInt(houseRentListShowBeans.getExtra().getPage().getPages()) == (Integer.parseInt(houseRentListParmarBean.getPage()) + 1)) {
            //如果当前获取的数据的条数小于当前每页显示条数，不能刷新了
            mRefreshLayout.setEnableLoadMore(false);//不能刷新了
        }
        houseRentListParmarBean.setPage(String.valueOf(houseRentListShowBeans.getExtra().getPage().getPage() + 1));
        if (contentAdapter.getData() == null || contentAdapter.getData().isEmpty()) {
            contentAdapter.setNewData(houseRentListShowBeans.getData());
        } else {
            contentAdapter.addData(houseRentListShowBeans.getData());
        }

    }
}
